/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pt.ua.miect.sd.simuladorpesca.ui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JTextField;

/**
 * Extends InputVerifier with regular expressions
 * @author PedroA
 */
public class RegexInputVerifier extends InputVerifier {
    private Pattern pattern;


    /**
     * Constructor
     * @param regex to be used to validate input
     */
    public RegexInputVerifier(String regex)
    {
        pattern = Pattern.compile(regex);
    }

    /**
     * Validates the input
     * @param input component to be validated
     * @return true if valid
     */
    @Override
    public boolean verify(JComponent input) {
       if (input instanceof JTextField)
       {
           JTextField txtField = (JTextField)input;
           Matcher m =  pattern.matcher(txtField.getText());
           return m.matches();
       }
       return false;
    }

}
